package com.easy.common.util;


import org.springframework.util.DigestUtils;

public class PasswordDeCode {
    private static int[] char_arr={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
    /**
     * 密码加密 随机盐值 随机次数
     * @param password
     * @return
     */
    public static String encryption(String password) {
        //加密次数
        int index_count=(int)(Math.random()*char_arr.length);
        //加密盐值
        int index_salt=(int)(Math.random()*char_arr.length);
        String result=password;
        for(int i=0;i<char_arr[index_count];i++){
            result=result+(char)char_arr[index_salt];
            result= DigestUtils.md5DigestAsHex(result.getBytes());

        }
        result=(char)char_arr[index_count]+result+(char)char_arr[index_salt];
        return result;
    }

    /**
     * 验证密码是否正确
     * @param password
     * @param decode
     * @return
     */
    public static boolean decode(String password,String decode) {
    char char_count=decode.charAt(0);
    char char_salt=decode.charAt(decode.length()-1);
    for(int i=0;i<char_count;i++){
        password=password+char_salt;
        password=DigestUtils.md5DigestAsHex(password.getBytes());
    }
    return decode.equals(char_count+password+char_salt);
    }
}
